package data
{
	/**
	 * This class is used to load in the dummy data for the data
	 * for the 3 graphs. 
	 */	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class LoadDummyData extends EventDispatcher
	{
		private static const URL			: String = "dummyData.xml";
		public static const XML_LOADED		: String = "XML.Loaded";
		
		private var _url				: String = "";
		private var _loader				: URLLoader;
		private var _xml				: XML;
		
		public function LoadDummyData()
		{
			// ...
		}
		
		/**
		 * Main method that goes out and grabs the XML
		 * data.
		 * 
		 * @param $url : String
		 */		
		public function init($url:String) : void
		{
			this._url = $url;
			_loader = new URLLoader();
			var XMLfile:File = File.applicationDirectory.resolvePath(URL);
			
			_loader.load(new URLRequest(XMLfile.url));
			_loader.addEventListener(Event.COMPLETE, _processXML);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, _onIOError);
		}
		
		/**
		 * Event processes the xml then dispatches a class event
		 * to let the listener of this class know that the data
		 * is ready.
		 * 
		 * @param $e : Event
		 */		
		private function _processXML($e : Event) : void
		{
			_xml = new XML($e.target.data);
			dispatchEvent(new Event(LoadDummyData.XML_LOADED));
		}
		
		private function _onIOError($e : IOErrorEvent) : void
		{
			trace ("Data did not load");
		}
		
		/**
		 * Return XML data.
		 * @return Xml 
		 * 
		 */	
		public function get data() : XML
		{
			return _xml;
		}
	}
}